/*   1:    */ package com.sie.driver.ftusbdriver.protocol;
/*   2:    */ 
/*   3:    */ import android.os.Handler;
/*   4:    */ import android.util.Log;
/*   5:    */ import java.util.LinkedList;
/*   6:    */ import java.util.Queue;
/*   7:    */ import java.util.concurrent.locks.Lock;
/*   8:    */ import java.util.concurrent.locks.ReentrantLock;
/*   9:    */ 
/*  10:    */ public abstract class SpiSlaveThread
/*  11:    */   extends Thread
/*  12:    */ {
/*  13:    */   public static final int THREAD_INIT = 0;
/*  14:    */   public static final int THREAD_RUNNING = 1;
/*  15:    */   public static final int THREAD_DESTORYED = 2;
/*  16:    */   private Handler m_pUIHandler;
/*  17:    */   private Queue<SpiSlaveEvent> m_pMsgQueue;
/*  18:    */   private Lock m_pMsgLock;
/*  19:    */   private Object m_pSendWaitCond;
/*  20:    */   private Object m_pResponseWaitCond;
/*  21:    */   private boolean m_bSendWaitCheck;
/*  22:    */   private boolean m_bResponseWaitCheck;
/*  23:    */   private int m_iThreadState;
/*  24:    */   
/*  25:    */   protected abstract boolean pollData();
/*  26:    */   
/*  27:    */   protected abstract void requestEvent(SpiSlaveEvent paramSpiSlaveEvent);
/*  28:    */   
/*  29:    */   protected abstract boolean isTerminateEvent(SpiSlaveEvent paramSpiSlaveEvent);
/*  30:    */   
/*  31:    */   public SpiSlaveThread()
/*  32:    */   {
/*  33: 39 */     this.m_pMsgQueue = new LinkedList();
/*  34: 40 */     this.m_pSendWaitCond = new Object();
/*  35: 41 */     this.m_pResponseWaitCond = new Object();
/*  36: 42 */     this.m_pMsgLock = new ReentrantLock();
/*  37: 43 */     this.m_iThreadState = 0;
/*  38: 44 */     setName("SpiSlaveThread");
/*  39:    */   }
/*  40:    */   
/*  41:    */   public boolean sendMessage(SpiSlaveEvent event)
/*  42:    */   {
/*  43: 49 */     while (this.m_iThreadState != 1) {
/*  44:    */       try
/*  45:    */       {
/*  46: 52 */         Thread.sleep(100L);
/*  47:    */       }
/*  48:    */       catch (InterruptedException localInterruptedException1) {}
/*  49:    */     }
/*  50: 58 */     this.m_pMsgLock.lock();
/*  51: 60 */     if (this.m_pMsgQueue.size() > 10)
/*  52:    */     {
/*  53: 62 */       this.m_pMsgLock.unlock();
/*  54: 63 */       Log.d("FTDI", "SpiSlaveThread sendMessage Buffer full!!");
/*  55: 64 */       return false;
/*  56:    */     }
/*  57: 66 */     this.m_pMsgQueue.add(event);
/*  58: 67 */     if (this.m_pMsgQueue.size() == 1) {
/*  59: 69 */       synchronized (this.m_pSendWaitCond)
/*  60:    */       {
/*  61: 71 */         this.m_bSendWaitCheck = true;
/*  62: 72 */         this.m_pSendWaitCond.notify();
/*  63:    */       }
/*  64:    */     }
/*  65: 75 */     this.m_pMsgLock.unlock();
/*  66: 76 */     if (event.getSync()) {
/*  67: 79 */       synchronized (this.m_pResponseWaitCond)
/*  68:    */       {
/*  69: 81 */         this.m_bResponseWaitCheck = false;
/*  70: 82 */         while (!this.m_bResponseWaitCheck) {
/*  71:    */           try
/*  72:    */           {
/*  73: 86 */             this.m_pResponseWaitCond.wait();
/*  74:    */           }
/*  75:    */           catch (InterruptedException e)
/*  76:    */           {
/*  77: 88 */             this.m_bResponseWaitCheck = true;
/*  78:    */           }
/*  79:    */         }
/*  80:    */       }
/*  81:    */     }
/*  82: 94 */     return true;
/*  83:    */   }
/*  84:    */   
/*  85:    */   public void run()
/*  86:    */   {
/*  87:101 */     boolean bQuit = false;
/*  88:    */     
/*  89:103 */     this.m_iThreadState = 1;
/*  90:104 */     while ((!Thread.interrupted()) && (!bQuit))
/*  91:    */     {
/*  92:106 */       pollData();
/*  93:107 */       this.m_pMsgLock.lock();
/*  94:110 */       if (this.m_pMsgQueue.size() <= 0)
/*  95:    */       {
/*  96:112 */         this.m_pMsgLock.unlock();
/*  97:    */       }
/*  98:    */       else
/*  99:    */       {
/* 100:116 */         SpiSlaveEvent event = (SpiSlaveEvent)this.m_pMsgQueue.peek();
/* 101:    */         
/* 102:118 */         this.m_pMsgQueue.remove();
/* 103:119 */         this.m_pMsgLock.unlock();
/* 104:    */         
/* 105:    */ 
/* 106:122 */         requestEvent(event);
/* 107:126 */         if (event.getSync()) {
/* 108:128 */           synchronized (this.m_pResponseWaitCond)
/* 109:    */           {
/* 110:130 */             while (this.m_bResponseWaitCheck) {
/* 111:    */               try
/* 112:    */               {
/* 113:133 */                 Thread.sleep(100L);
/* 114:    */               }
/* 115:    */               catch (InterruptedException e)
/* 116:    */               {
/* 117:134 */                 bQuit = true;
/* 118:    */               }
/* 119:    */             }
/* 120:136 */             this.m_bResponseWaitCheck = true;
/* 121:    */             
/* 122:138 */             this.m_pResponseWaitCond.notify();
/* 123:    */           }
/* 124:    */         }
/* 125:142 */         bQuit = isTerminateEvent(event);
/* 126:    */       }
/* 127:    */     }
/* 128:144 */     this.m_iThreadState = 2;
/* 129:    */   }
/* 130:    */ }



/* Location:           E:\code\demo\A108DevForBankComm\libs\d2xx.jar

 * Qualified Name:     com.ftdi.j2xx.protocol.SpiSlaveThread

 * JD-Core Version:    0.7.0.1

 */